//
//  SettingViewController.m
//  ReplaceColor
//
//  Created by wzv on 15/1/25.
//  Copyright (c) 2015年 wzv. All rights reserved.
//

#import "SettingViewController.h"
#import "sys/utsname.h"
#import <MessageUI/MessageUI.h>
#import "MobClick.h"
#import "EBPurchase.h"

#define productID @"com.jinWu.ReplaceColor"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource,MFMailComposeViewControllerDelegate,UIAlertViewDelegate,EBPurchaseDelegate>

@property(nonatomic,retain) UITableView *table;
@property(nonatomic,retain) EBPurchase* myEBPurchase;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    UIColor *navTitleColor = [UIColor whiteColor];
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
        navTitleColor = [UIColor blackColor];
    }
    
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                 target:self
                                                                                 action:@selector(goToBack)];
    self.navigationItem.rightBarButtonItem = rightBtnItem;
    [rightBtnItem release];
    
    UIButton *titltBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    titltBtn.frame = CGRectMake(0, 0, 200, 44);
    [titltBtn setTitle:NSLocalizedString(@"setting",nil) forState:UIControlStateNormal];
    titltBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
    [titltBtn setTitleColor:navTitleColor forState:UIControlStateNormal];
    self.navigationItem.titleView = titltBtn;
    
    UITableView *tempTable = [[UITableView alloc]initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height) style:UITableViewStyleGrouped];
    tempTable.delegate = self;
    tempTable.dataSource = self;
    tempTable.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tempTable];
    self.table = tempTable;
    [tempTable release];
    
    self.myEBPurchase = [[[EBPurchase alloc] init] autorelease];
    self.myEBPurchase.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self resetAllSubViewFrame];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self resetAllSubViewFrame];
}

- (void)resetAllSubViewFrame
{
    self.table.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    self.table = nil;
    self.myEBPurchase = nil;
    
    [super dealloc];
}

- (void)goToBack
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 40;
    }
    
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *textCell = @"textCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:textCell];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:textCell] autorelease];
    }
    else
    {
        for (UIView*temp in [cell.contentView subviews])
        {
            [temp removeFromSuperview];
        }
    }
    
    NSInteger section = [indexPath section];
    switch (section)
    {
        case 0:
        {
            cell.textLabel.text = NSLocalizedString(@"update_version",@"");
            break;
        }
        case 1:
        {
            cell.textLabel.text = NSLocalizedString(@"remove_ad",@"");
            break;
        }
        case 2:
        {
            cell.textLabel.text = NSLocalizedString(@"feedback_title",@"");
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        }
        case 3:
        {
            cell.textLabel.text = NSLocalizedString(@"rate",@"");
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        }
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSInteger section = [indexPath section];
    switch (section)
    {
        case 0:
        {//版本升级
            [MobClick checkUpdateWithDelegate:self selector:@selector(actionMobClickCallBack:)];
            break;
        }
        case 1:
        {//去广告
            [MobClick event:@"removeAd"];
            
            BOOL isNotShowAds = [[NSUserDefaults standardUserDefaults] boolForKey:@"KReplaceColorRemoveAds"];
            if (isNotShowAds)
            {
                NSString *msg = NSLocalizedString(@"IAP_HasBuy",@"");
                NSString *cancelButtonTitle = NSLocalizedString(@"comfirm",@"");
                
                if (IOS_VERSION_8_OR_ABOVE)
                {
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    }];
                    
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                             message:msg
                                                                                      preferredStyle:UIAlertControllerStyleAlert];
                    [alertController addAction:cancelAction];
                    [self presentViewController:alertController animated:YES completion:nil];
                }
                else
                {
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                                       message:msg
                                                                      delegate:self
                                                             cancelButtonTitle:cancelButtonTitle
                                                             otherButtonTitles:nil];
                    alertView.tag = 2009;
                    [alertView show];
                    [alertView release];
                }
            }
            else
            {
                if (![self.myEBPurchase requestProduct:productID])
                {
                    [self showDisabledBuyTip];
                }
            }

            break;
        }
        case 2:
        {//发邮件
            //获取设备名称
            struct utsname systemInfo;
            uname(&systemInfo);
            NSString *currentDevice = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
            
            //设置主题
            NSString *feedBackTitle = NSLocalizedString(@"feedback_title",@"");
            NSString *feedBackBody = NSLocalizedString(@"feedback_body",@"");
            
            NSString *currentVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleVersion"];
            
            NSString *msgBody = [NSString stringWithFormat:@"\n\n(%@%@;%@;%@)",feedBackBody,currentDevice,[[UIDevice currentDevice] systemVersion],currentVersion];
            
            //添加收件人
            NSArray *toRecipients = [NSArray arrayWithObject: @"wuzv333@gmail.com"];
            
            Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
            if (mailClass != nil)
            {
                if ([mailClass canSendMail])
                {
                    //调用自带邮件系统发送邮件
                    [self displayComposerSheet:feedBackTitle
                                  toRecipients:toRecipients
                                       msgBody:msgBody
                            attachmentDataPath:nil];
                }
                else
                {
                    [self launchMailAppOnDevice:feedBackTitle
                                   toRecipients:toRecipients
                                        msgBody:msgBody];
                }
            }
            else
            {
                [self launchMailAppOnDevice:feedBackTitle
                               toRecipients:toRecipients
                                    msgBody:msgBody];
            }
            break;
        }
        case 3:
        {
            NSString *str = nil;
            if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
            {
                str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%d",969946453];
            }
            else
            {
                str = [NSString stringWithFormat:
                       @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d",969946453];
                
            }
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            break;
        }
        default:
            break;
    }
}

#pragma mark - MobClickCallBack

- (void)actionMobClickCallBack:(id)sender
{
    BOOL isUpdate = [[sender objectForKey:@"update"] boolValue];
    if (isUpdate)
    {
        //提取服务器返回的信息，弹出提示框
        NSString *path = [sender objectForKey:@"path"];
        [[NSUserDefaults standardUserDefaults] setObject:path forKey:@"kUpdateLinkForPlistKey"];
        NSString *version = [sender objectForKey:@"version"];
        
        NSString *tipTitle = NSLocalizedString(@"VERSION_UPDATE_TIP_TITLE",@"");
        NSString *tipMsg = NSLocalizedString(@"VERSION_UPDATE_TIP_MSG",@"");
        NSString *tipCancelBtnTitle = NSLocalizedString(@"VERSION_UPDATE_TIP_CANCELBTN",@"");
        NSString *tipOtherBtnTitle = NSLocalizedString(@"VERSION_UPDATE_TIP_OTHERBTN",@"");
        
        if (IOS_VERSION_8_OR_ABOVE)
        {
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:tipCancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            
            UIAlertAction *updateAction = [UIAlertAction actionWithTitle:tipOtherBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSString *path = [[NSUserDefaults standardUserDefaults] objectForKey:@"kUpdateLinkForPlistKey"];
                if (path.length >0)
                {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:path]];
                }
            }];
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:tipTitle
                                                                                     message:[NSString stringWithFormat:tipMsg,version]
                                                                              preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:cancelAction];
            [alertController addAction:updateAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        else
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:tipTitle
                                                               message:[NSString stringWithFormat:tipMsg,version]
                                                              delegate:self
                                                     cancelButtonTitle:tipCancelBtnTitle
                                                     otherButtonTitles:tipOtherBtnTitle,nil];
            alertView.tag = 1000;
            [alertView show];
            [alertView release];
        }
    }
    else
    {
        NSString *cancelButtonTitle = NSLocalizedString(@"comfirm", nil);
        NSString *tipMsg = NSLocalizedString(@"VERSION_NO_UPDATE_TIP_MSG",@"");
        NSString *version = [sender objectForKey:@"current_version"];
        
        if (IOS_VERSION_8_OR_ABOVE)
        {
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                     message:[NSString stringWithFormat:tipMsg,version]
                                                                              preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
            
            [UIApplication sharedApplication].applicationIconBadgeNumber = 0;   // hide
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"kDispIconUpdateWarn"];
        }
        else
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                               message:[NSString stringWithFormat:tipMsg,version]
                                                              delegate:self
                                                     cancelButtonTitle:cancelButtonTitle
                                                     otherButtonTitles:nil];
            alertView.tag = 1001;
            [alertView show];
            [alertView release];
        }
    }
}

#pragma mark - FeedBack
//系统已有配置好的邮箱账户，可以发送邮件
-(void)displayComposerSheet:(NSString *)subject
               toRecipients:(NSArray *)recipients
                    msgBody:(NSString *)body
         attachmentDataPath:(NSString *)path
{
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    mailPicker.mailComposeDelegate = self;
    [mailPicker setSubject:subject];
    [mailPicker setMessageBody:body isHTML:NO];
    [mailPicker setToRecipients:recipients];
    if(path.length >0)
    {
        NSData *attachmentData = [NSData dataWithContentsOfFile:path];
        [mailPicker addAttachmentData:attachmentData
                             mimeType:[path pathExtension]
                             fileName:[path lastPathComponent]];
    }
    [self presentViewController: mailPicker animated:YES completion:NULL];
    [mailPicker release];
}

//没有设置好的邮箱账户，调用系统方法
-(void)launchMailAppOnDevice:(NSString *)subject
                toRecipients:(NSArray *)recipients
                     msgBody:(NSString *)body
{
    
    NSString *email = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@",recipients,subject,body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

//邮件发送完成的回调函数
- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error
{
    NSString *msg = nil;
    switch (result)
    {
        case MFMailComposeResultSaved:
        {
            msg = NSLocalizedString(@"feedback_mail_save",@"");
            [self alertWithTitle:nil msg:msg];
            break;
        }
        case MFMailComposeResultSent:
        {
            msg = NSLocalizedString(@"feedback_mail_send",@"");
            [self alertWithTitle:nil msg:msg];
            break;
        }
        case MFMailComposeResultFailed:
        {
            msg = NSLocalizedString(@"feedback_mail_fail",@"");
            [self alertWithTitle:nil msg:msg];
            break;
        }
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:NULL];
}

//邮件发送完成后弹出的提示框
- (void) alertWithTitle:(NSString *)title msg:(NSString *)msg
{
    NSString *cancelButtonTitle = NSLocalizedString(@"comfirm", nil);
    
    if (IOS_VERSION_8_OR_ABOVE)
    {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                 message:msg
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                           message:msg
                                                          delegate:self
                                                 cancelButtonTitle:cancelButtonTitle
                                                 otherButtonTitles:nil];
        alertView.tag = 1002;
        [alertView show];
        [alertView release];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (1 == buttonIndex && alertView.tag == 1000)
    {
        NSString *path = [[NSUserDefaults standardUserDefaults] objectForKey:@"kUpdateLinkForPlistKey"];
        if (path.length >0)
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:path]];
        }
    }
}

#pragma mark 应用内购买

-(void)purchaseProduct
{
    if (self.myEBPurchase.validProduct != nil)
    {
        if (![self.myEBPurchase purchaseProduct:self.myEBPurchase.validProduct])
        {
            [self showDisabledBuyTip];
        }
    }
}

-(void)restorePurchase
{
    if (![self.myEBPurchase restorePurchase])
    {
        [self showDisabledBuyTip];
    }
}

- (void)showDisabledBuyTip
{
    NSString *msg = NSLocalizedString(@"IAP_Disabled",@"");
    NSString *cancelButtonTitle = NSLocalizedString(@"comfirm",@"");
    
    if (IOS_VERSION_8_OR_ABOVE)
    {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                 message:msg
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                           message:msg
                                                          delegate:self
                                                 cancelButtonTitle:cancelButtonTitle
                                                 otherButtonTitles:nil];
        alertView.tag = 2002;
        [alertView show];
        [alertView release];
    }
}

#pragma mark -
#pragma mark EBPurchaseDelegate Methods

-(void) requestedProduct:(EBPurchase*)ebp identifier:(NSString*)productId name:(NSString*)productName price:(NSString*)productPrice description:(NSString*)productDescription
{
    if (productPrice != nil)
    {
        NSString *msg = [NSString stringWithFormat:@"%@\n%@ %@",productDescription,NSLocalizedString(@"IAP_Price",@""),productPrice];
        NSString *cancelButtonTitle = NSLocalizedString(@"cancel",@"");
        NSString *buyButtonTitle = NSLocalizedString(@"IAP_Buy",@"");
        NSString *restoreButtonTitle = NSLocalizedString(@"IAP_Restore",@"");
        
        if (IOS_VERSION_8_OR_ABOVE)
        {
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            
            UIAlertAction *buyAction = [UIAlertAction actionWithTitle:buyButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [self purchaseProduct];
            }];
            
            UIAlertAction *restoreAction = [UIAlertAction actionWithTitle:restoreButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [self restorePurchase];
            }];
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                     message:msg
                                                                              preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:cancelAction];
            [alertController addAction:buyAction];
            [alertController addAction:restoreAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
        else
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                               message:msg
                                                              delegate:self
                                                     cancelButtonTitle:cancelButtonTitle
                                                     otherButtonTitles:buyButtonTitle,restoreButtonTitle,nil];
            alertView.tag = 2003;
            [alertView show];
            [alertView release];
        }
    }
    else
    {
        NSString *msg = NSLocalizedString(@"IAP_List_Fail",@"");
        NSString *cancelButtonTitle = NSLocalizedString(@"comfirm",@"");
        
        if (IOS_VERSION_8_OR_ABOVE)
        {
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                     message:msg
                                                                              preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        else
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                               message:msg
                                                              delegate:self
                                                     cancelButtonTitle:cancelButtonTitle
                                                     otherButtonTitles:nil];
            alertView.tag = 2004;
            [alertView show];
            [alertView release];
        }
    }
}


-(void) successfulPurchase:(EBPurchase*)ebp identifier:(NSString*)productId receipt:(NSData*)transactionReceipt
{
    BOOL isNotShowAds = [[NSUserDefaults standardUserDefaults] boolForKey:@"KReplaceColorRemoveAds"];
    if (!isNotShowAds)
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"KReplaceColorRemoveAds"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        NSString *msg = NSLocalizedString(@"IAP_Buy_OK",@"");
        NSString *cancelButtonTitle = NSLocalizedString(@"comfirm",@"");
        
        if (IOS_VERSION_8_OR_ABOVE)
        {
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                     message:msg
                                                                              preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        else
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                               message:msg
                                                              delegate:self
                                                     cancelButtonTitle:cancelButtonTitle
                                                     otherButtonTitles:nil];
            alertView.tag = 2005;
            [alertView show];
            [alertView release];
        }
    }
}

-(void) failedPurchase:(EBPurchase*)ebp error:(NSInteger)errorCode message:(NSString*)errorMessage
{
    NSString *msg = NSLocalizedString(@"IAP_Buy_Fail",@"");
    NSString *cancelButtonTitle = NSLocalizedString(@"comfirm",@"");
    
    if (IOS_VERSION_8_OR_ABOVE)
    {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                 message:msg
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                           message:msg
                                                          delegate:self
                                                 cancelButtonTitle:cancelButtonTitle
                                                 otherButtonTitles:nil];
        alertView.tag = 2006;
        [alertView show];
        [alertView release];
    }
}

-(void) incompleteRestore:(EBPurchase*)ebp
{
    NSString *msg = NSLocalizedString(@"IAP_Restore_NotFind",@"");
    NSString *cancelButtonTitle = NSLocalizedString(@"comfirm",@"");
    
    if (IOS_VERSION_8_OR_ABOVE)
    {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                 message:msg
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                           message:msg
                                                          delegate:self
                                                 cancelButtonTitle:cancelButtonTitle
                                                 otherButtonTitles:nil];
        alertView.tag = 2007;
        [alertView show];
        [alertView release];
    }
}

-(void) failedRestore:(EBPurchase*)ebp error:(NSInteger)errorCode message:(NSString*)errorMessage
{
    NSString *msg = NSLocalizedString(@"IAP_Restore_Fail",@"");
    NSString *cancelButtonTitle = NSLocalizedString(@"comfirm",@"");
    
    if (IOS_VERSION_8_OR_ABOVE)
    {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                                 message:msg
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                           message:msg
                                                          delegate:self
                                                 cancelButtonTitle:cancelButtonTitle
                                                 otherButtonTitles:nil];
        alertView.tag = 2008;
        [alertView show];
        [alertView release];
    }
}

@end
